package com.xueyu.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.dao.OrderapplyDao;
import com.xueyu.dao.SupplierDao;
import com.xueyu.dao.drugsDao;
import com.xueyu.domain.SupplierBean;
import com.xueyu.domain.drugs.drugsBean;
import com.xueyu.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.xueyu.dao.DrugDao;
import com.xueyu.domain.drug.drug;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/Drug")
public class DrugController {
    @Autowired
    DrugDao DrugDao;
    @Autowired
    com.xueyu.dao.drugsDao drugsDao;
    @Autowired
    com.xueyu.dao.OrderapplyDao OrderapplyDao;
    @Autowired
    com.xueyu.dao.SupplierDao SupplierDao;
    @RequestMapping(value = "/add")
    public  ResultBean add(HttpServletRequest request, drug drug, @RequestPart(value = "pic", required = false) MultipartFile[] pic) throws Exception{
        ResultBean resultBean = new ResultBean();
        if(pic!=null) {
            for (MultipartFile file : pic) {
                String picName = UUID.randomUUID().toString();
                // 截取文件的扩展名(如.jpg)
                String oriName = file.getOriginalFilename();
                String extName = oriName.substring(oriName.lastIndexOf("."));
                String path = request.getSession().getServletContext().getRealPath("\\");
                //日期处理
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String sdate = sdf.format(date);
                String dirName = "\\upload\\" + sdate + "\\";
                File dirFile = new File(path + dirName);
                if (!dirFile.exists()) {
                    dirFile.mkdirs();
                }
                String newFileName = picName + extName;
                File targetFile = new File(path + dirName, newFileName);
                file.transferTo(targetFile);
                String path1 = dirName + newFileName;
                System.out.println("path=" + path1);

                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String currentTime = sdf1.format(date);
                drug.setDrugPic(path1);
            }
        }
        if(drug.getDrugID()==0) {
            Date date = new Date();
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String currentTime = sdf1.format(date);
            drug.setCtime(currentTime);
            drug.setGtime(currentTime);
            DrugDao.insert(drug);
        }else {
            Date date = new Date();
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String currentTime = sdf1.format(date);
            drug.setGtime(currentTime);
            DrugDao.updateById(drug);
        }
        return resultBean;
    }
    //分页.条件查询.
    @RequestMapping("/fy")
    public ResultBean fy(@RequestBody Map map) {
        int current = (Integer)map.get("Current");
        int size = (Integer)map.get("size");
        Page<drug> page = new Page<>(current, size);
        QueryWrapper<drug> queryWrapper = createSchoolQueryWrapper(map);
        page = DrugDao.selectPage(page, queryWrapper);
        List<drug> list = page.getRecords();
        for (drug Drug : list) {
            // 获取药品的分类id
            Long categoryId = Long.valueOf(Drug.getDrugF());
            Long categoryIds = Long.valueOf(Drug.getSupplier());
            // 根据分类id查询对应的分类信息
            drugsBean category = drugsDao.selectById(categoryId);
            SupplierBean category2 = SupplierDao.selectById(categoryIds);
            category2.setSuppliers(String.valueOf(2)); // 添加查询条件
            // 如果分类存在，则将分类名称设置到药品表中
            if (category != null) {
                Drug.setP(category.getDrugNames());
            }
            if (category2 != null) {
                Drug.setG(category2.getSupplierName());
            }
        }
         ResultBean resultBean = new ResultBean();
        resultBean.put("page",page);
        resultBean.put("list",list);
        return resultBean;
    }
    //删除
    @RequestMapping("/del/{DrugID}")
    public ResultBean del(@PathVariable("DrugID") int DrugID){
        DrugDao.deleteById(DrugID);
        return new ResultBean();
    }

    @RequestMapping("/zd")
    public ResultBean zd(@RequestBody  Map map)
    {
        QueryWrapper<drug> queryWrapper = createSchoolQueryWrapper(map);
        List<drug> list = DrugDao.selectList(queryWrapper);
        ResultBean resultBean = new ResultBean();
        resultBean.put("list",list);
        return resultBean;
    }




    @RequestMapping("/getid/{DrugID}")
    public ResultBean getid(@PathVariable("DrugID") int DrugID){
        ResultBean resultBean = new ResultBean();
        if (DrugID>0) {
            QueryWrapper<drug> queryWrapper = Wrappers.query();
            queryWrapper.eq("DrugID", DrugID);
            List<drug> DrugList = DrugDao.selectList(queryWrapper);

            for (drug Drug : DrugList) {
                // 获取药品的分类id
                Long categoryId = Long.valueOf(Drug.getDrugF());
                Long categoryIds = Long.valueOf(Drug.getSupplier());
                // 根据分类id查询对应的分类信息
                drugsBean category = drugsDao.selectById(categoryId);
                SupplierBean category2 = SupplierDao.selectById(categoryIds);
                // 如果分类存在，则将分类名称设置到药品表中
                if (category != null) {
                    Drug.setP(category.getDrugNames());
                }
                if (category2 != null) {
                    Drug.setG(category2.getSupplierName());
                }
            }
            resultBean.put("data", DrugList);
        }
        return resultBean;
    }

    //封装好的查询条件
    public QueryWrapper<drug> createSchoolQueryWrapper(Map map) {
        String DrugID =  (String)map.get("drugID");
        String DrugName =  (String)map.get("drugName");
        String DrugF =  (String)map.get("drugF");
        String g =  (String)map.get("g");
        QueryWrapper<drug> queryWrapper = Wrappers.query();
        if (DrugID != null && !"".equals(DrugID)) {
            queryWrapper.eq("DrugID", DrugID);
        }
        if (DrugName != null && !"".equals(DrugName)) {
            queryWrapper.like("DrugName", "%" + DrugName + "%");
        }
        if (DrugF != null && !"".equals(DrugF)) {
            queryWrapper.like("DrugF",  DrugF);
        }
        if (g != null && !"".equals(g)) {
            queryWrapper.like("supplier",  g);
        }
        return queryWrapper;
    }
}
